package com.zj.java21demo.exception;

import com.zj.java21demo.utils.ResultVoUtil;
import com.zj.java21demo.vo.ResultVo;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 全局异常捕获处理
 * @Version 1.0.0
 * @Date 2024/2/28 16:27
 * @Created by zhangjun
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AuthorizationExceptionEx.class)
    public ResultVo authorizationExceptionHandling(AuthorizationExceptionEx ex) {
        System.out.println("authorizationExceptionHandling = " + ex);
        return ResultVoUtil.error(1000, ex.getMessage());
    }

    // handling specific exception
    @ExceptionHandler(ServerException.class)
    public ResultVo serverExceptionHandling(ServerException ex) {
        System.out.println("serverExceptionHandling = " + ex);
        return ResultVoUtil.error(6000, ex.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResultVo accessDeniedExceptionHandling(AccessDeniedException ex) {
        System.out.println("accessDeniedExceptionHandling = " + ex);
        return ResultVoUtil.error(403, "权限不足");
    }

    // handling global exception
    @ExceptionHandler(Exception.class)
    public ResultVo exceptionHandling(Exception ex) {
        System.out.println("exceptionHandling = " + ex);
        return ResultVoUtil.error(500, "服务器内部异常,请稍后重试");
    }
}
